package e_routing_topic;

import com.rabbitmq.client.*;
import org.junit.Test;
import utils.RabbitMQUtils;

import java.io.IOException;

public class Consumer2 {
    @Test
    public void receiveMessage() throws Exception {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        String exchangeName = "topic";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC);
        //得到一个临时队列
        String queue = channel.queueDeclare().getQueue();
        //绑定队列到交换机
        channel.queueBind(queue, exchangeName, "user.#");
        //消费消息
        channel.basicConsume(queue, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者【2】收到消息:" + new String(body));
            }
        });
        System.in.read();
    }
}